package com.ruoyi.ggsearch.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ggsearch.mapper.GgDomainMapper;
import com.ruoyi.ggsearch.domain.GgDomain;
import com.ruoyi.ggsearch.service.IGgDomainService;

import javax.validation.Validator;

/**
 * 域名信息Service业务层处理
 * 
 * @author yangjin
 * @date 2023-04-07
 */
@Service
public class GgDomainServiceImpl implements IGgDomainService 
{
    private static final Logger log = LoggerFactory.getLogger(GgDomainServiceImpl.class);
    @Autowired
    private GgDomainMapper ggDomainMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询域名信息
     * 
     * @param keyId 域名信息主键
     * @return 域名信息
     */
    @Override
    public GgDomain selectGgDomainByKeyId(Long keyId)
    {
        return ggDomainMapper.selectGgDomainByKeyId(keyId);
    }

    /**
     * 查询域名信息列表
     * 
     * @param ggDomain 域名信息
     * @return 域名信息
     */
    @Override
    public List<GgDomain> selectGgDomainList(GgDomain ggDomain)
    {
        return ggDomainMapper.selectGgDomainList(ggDomain);
    }

    /**
     * 新增域名信息
     * 
     * @param ggDomain 域名信息
     * @return 结果
     */
    @Override
    public int insertGgDomain(GgDomain ggDomain)
    {
        return ggDomainMapper.insertGgDomain(ggDomain);
    }

    /**
     * 修改域名信息
     * 
     * @param ggDomain 域名信息
     * @return 结果
     */
    @Override
    public int updateGgDomain(GgDomain ggDomain)
    {
        return ggDomainMapper.updateGgDomain(ggDomain);
    }

    /**
     * 批量删除域名信息
     * 
     * @param keyIds 需要删除的域名信息主键
     * @return 结果
     */
    @Override
    public int deleteGgDomainByKeyIds(Long[] keyIds)
    {
        return ggDomainMapper.deleteGgDomainByKeyIds(keyIds);
    }

    /**
     * 删除域名信息信息
     * 
     * @param keyId 域名信息主键
     * @return 结果
     */
    @Override
    public int deleteGgDomainByKeyId(Long keyId)
    {
        return ggDomainMapper.deleteGgDomainByKeyId(keyId);
    }



    /**
     * 导入域名数据
     *
     * @param domainList 域名数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDomain(List<GgDomain> domainList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(domainList) || domainList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GgDomain domain : domainList)
        {
            try
            {
                // 验证是否存在这个域名
                GgDomain d = ggDomainMapper.selectGgDomainByName(domain.getDomain());
                if (StringUtils.isNull(d))
                {
                    BeanValidators.validateWithException(validator, domain);
                    ggDomainMapper.insertGgDomain(domain);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、域名 " + domain.getDomain() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, domain);
                    ggDomainMapper.updateGgDomain(domain);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、域名 " + domain.getDomain() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、域名 " + domain.getDomain() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、域名 " + domain.getDomain() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    @Override
    public void deleteGgDomainAll()
    {
        ggDomainMapper.deleteGgDomainAll();
    }
}
